package hsb.ruoyi.admin.web.controller.system;

import hsb.ruoyi.common.annotation.Log;
import hsb.ruoyi.common.constant.UserConstants;
import hsb.ruoyi.common.core.controller.BaseController;
import hsb.ruoyi.common.core.domain.AjaxResult;
import hsb.ruoyi.common.core.page.TableDataInfo;
import hsb.ruoyi.common.enums.BusinessType;
import hsb.ruoyi.system.domain.SysConfig;
import hsb.ruoyi.system.service.ISysConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.validation.Valid;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;

/**
 * 参数配置 信息操作处理
 *
 * @author ruoyi
 */

@Path("/system/config")
@Produces(MediaType.APPLICATION_JSON)
public class SysConfigController extends BaseController {


    private final Logger log = LoggerFactory.getLogger(SysConfigController.class);

    @Inject
    private ISysConfigService configService;


    /**
     * 获取参数配置列表
     */
    //@PreAuthorize("@ss.hasPermi('system:config:list')")

    @GET
    @Path("/list")
    public TableDataInfo list(@BeanParam SysConfig config) {
        startPage();
        List<SysConfig> list = configService.selectConfigList(config);
        return getDataTable(list);
    }


//    @Log(title = "参数管理", businessType = BusinessType.EXPORT)
//    @PreAuthorize("@ss.hasPermi('system:config:export')")
//    @POST @Path("/export")
//    public void export(HttpServletResponse response, SysConfig config)
//    {
//        List<SysConfig> list = configService.selectConfigList(config);
//        ExcelUtil<SysConfig> util = new ExcelUtil<SysConfig>(SysConfig.class);
//        util.exportExcel(response, list, "参数数据");
//    }

    /**
     * 根据参数编号获取详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:config:query')")
    @GET
    @Path(value = "/{configId}")
    public AjaxResult getInfo(@PathParam("configId") Long configId) {
        return AjaxResult.success(configService.selectConfigById(configId));
    }

    /**
     * 根据参数键名查询参数值
     */
    @GET
    @Path(value = "/configKey/{configKey}")
    public AjaxResult getConfigKey(@PathParam("configKey") String configKey) {
        return AjaxResult.success(configService.selectConfigByKey(configKey));
    }

    /**
     * 新增参数配置
     */
    //@PreAuthorize("@ss.hasPermi('system:config:add')")
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @POST
    //@Path
    public AjaxResult add(@Valid SysConfig config) {
        if (UserConstants.NOT_UNIQUE.equals(configService.checkConfigKeyUnique(config))) {
            return AjaxResult.error("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setCreateBy(getUsername());
        return toAjax(configService.insertConfig(config));
    }



    /**
     * 修改参数配置
     */
    //@PreAuthorize("@ss.hasPermi('system:config:edit')")
    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @PUT
    public AjaxResult edit(@Valid SysConfig config) {
        if (UserConstants.NOT_UNIQUE.equals(configService.checkConfigKeyUnique(config))) {
            return AjaxResult.error("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setUpdateBy(getUsername());
        return toAjax(configService.updateConfig(config));
    }

    /**
     * 删除参数配置
     */
    //@PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "参数管理", businessType = BusinessType.DELETE)
    @DELETE
    @Path("/{configIds}")
    public AjaxResult remove(@PathParam("configIds") List<Long> configIds) {
        configService.deleteConfigByIds(configIds.toArray(new Long[]{}));
        return success();
    }

    /**
     * 刷新参数缓存
     */
    //@PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "参数管理", businessType = BusinessType.CLEAN)
    @DELETE
    @Path("/refreshCache")
    public AjaxResult refreshCache() {
        configService.resetConfigCache();
        return AjaxResult.success();
    }
}
